<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Python列表、元组、字典单元测试</title>
    <script src="https://cdn.tailwindcss.com/3.3.3"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.7.2/css/all.min.css">
    <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.7.0/highlight.min.js"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.7.0/styles/atom-one-light.min.css">
    <style>
        body {
            font-family: 'Noto Sans SC', sans-serif;
            background-color: #f8fafc;
            color: #1e293b;
        }
        .question-card {
            transition: all 0.3s ease;
            background-color: white;
            border-radius: 0.5rem;
            box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
        }
        .question-card:hover {
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
            transform: translateY(-2px);
        }
        .code-block {
            background-color: #f6f8fa;
            border-radius: 0.375rem;
            padding: 1rem;
            font-family: 'Courier New', Courier, monospace;
        }
        .answer-input {
            border: 1px solid #e2e8f0;
            border-radius: 0.375rem;
            padding: 0.5rem 0.75rem;
            transition: border-color 0.2s;
        }
        .answer-input:focus {
            outline: none;
            border-color: #6366f1;
            box-shadow: 0 0 0 2px rgba(99, 102, 241, 0.2);
        }
        .submit-btn {
            background-color: #6366f1;
            color: white;
            transition: all 0.2s;
        }
        .submit-btn:hover {
            background-color: #4f46e5;
            transform: translateY(-1px);
        }
        .result-card {
            background-color: #f0fdf4;
            border-left: 4px solid #10b981;
        }
        .radio-option {
            display: flex;
            align-items: center;
            padding: 0.5rem;
            border-radius: 0.375rem;
            transition: background-color 0.2s;
        }
        .radio-option:hover {
            background-color: #f1f5f9;
        }
        .radio-option input {
            margin-right: 0.75rem;
        }
    </style>
</head>
<body class="min-h-screen">
    <div class="container mx-auto px-4 py-8 max-w-4xl">
        <div class="text-center mb-8">
            <h1 class="text-3xl font-bold text-gray-800 mb-2">Python列表、元组、字典单元测试</h1>
            <p class="text-gray-600">请认真作答，完成后点击提交按钮查看成绩</p>
        </div>

        <form id="examForm">
            <!-- 选择题部分 -->
            <div class="question-card p-6 mb-8">
                <h2 class="text-xl font-semibold mb-4 text-gray-800 border-b pb-2">一、选择题（每题5分，共30分）</h2>
                
                <div class="mb-6">
                    <p class="font-medium mb-3">1. 下列哪种数据类型在Python中是不可变的？</p>
                    <div class="space-y-2">
                        <label class="radio-option">
                            <input type="radio" name="q1" value="A" class="text-indigo-600"> A. 列表
                        </label>
                        <label class="radio-option">
                            <input type="radio" name="q1" value="B" class="text-indigo-600"> B. 元组
                        </label>
                        <label class="radio-option">
                            <input type="radio" name="q1" value="C" class="text-indigo-600"> C. 字典
                        </label>
                        <label class="radio-option">
                            <input type="radio" name="q1" value="D" class="text-indigo-600"> D. 集合
                        </label>
                    </div>
                </div>

                <div class="mb-6">
                    <p class="font-medium mb-3">2. 以下哪个符号用于创建列表？</p>
                    <div class="space-y-2">
                        <label class="radio-option">
                            <input type="radio" name="q2" value="A" class="text-indigo-600"> A. ()
                        </label>
                        <label class="radio-option">
                            <input type="radio" name="q2" value="B" class="text-indigo-600"> B. {}
                        </label>
                        <label class="radio-option">
                            <input type="radio" name="q2" value="C" class="text-indigo-600"> C. []
                        </label>
                        <label class="radio-option">
                            <input type="radio" name="q2" value="D" class="text-indigo-600"> D. <>
                        </label>
                    </div>
                </div>

                <div class="mb-6">
                    <p class="font-medium mb-3">3. 字典中的元素是通过什么来访问的？</p>
                    <div class="space-y-2">
                        <label class="radio-option">
                            <input type="radio" name="q3" value="A" class="text-indigo-600"> A. 索引
                        </label>
                        <label class="radio-option">
                            <input type="radio" name="q3" value="B" class="text-indigo-600"> B. 键
                        </label>
                        <label class="radio-option">
                            <input type="radio" name="q3" value="C" class="text-indigo-600"> C. 位置
                        </label>
                        <label class="radio-option">
                            <input type="radio" name="q3" value="D" class="text-indigo-600"> D. 序号
                        </label>
                    </div>
                </div>

                <div class="mb-6">
                    <p class="font-medium mb-3">4. 下列哪个方法可以向列表末尾添加元素？</p>
                    <div class="space-y-2">
                        <label class="radio-option">
                            <input type="radio" name="q4" value="A" class="text-indigo-600"> A. add()
                        </label>
                        <label class="radio-option">
                            <input type="radio" name="q4" value="B" class="text-indigo-600"> B. append()
                        </label>
                        <label class="radio-option">
                            <input type="radio" name="q4" value="C" class="text-indigo-600"> C. insert()
                        </label>
                        <label class="radio-option">
                            <input type="radio" name="q4" value="D" class="text-indigo-600"> D. extend()
                        </label>
                    </div>
                </div>

                <div class="mb-6">
                    <p class="font-medium mb-3">5. 元组的创建使用什么符号？</p>
                    <div class="space-y-2">
                        <label class="radio-option">
                            <input type="radio" name="q5" value="A" class="text-indigo-600"> A. []
                        </label>
                        <label class="radio-option">
                            <input type="radio" name="q5" value="B" class="text-indigo-600"> B. {}
                        </label>
                        <label class="radio-option">
                            <input type="radio" name="q5" value="C" class="text-indigo-600"> C. ()
                        </label>
                        <label class="radio-option">
                            <input type="radio" name="q5" value="D" class="text-indigo-600"> D. <>
                        </label>
                    </div>
                </div>

                <div class="mb-6">
                    <p class="font-medium mb-3">6. 以下代码的输出结果是？</p>
                    <pre class="code-block mb-3"><code class="language-python">my_dict = {'name': '小明', 'age': 12}
print(my_dict['name'])</code></pre>
                    <div class="space-y-2">
                        <label class="radio-option">
                            <input type="radio" name="q6" value="A" class="text-indigo-600"> A. 小明
                        </label>
                        <label class="radio-option">
                            <input type="radio" name="q6" value="B" class="text-indigo-600"> B. 12
                        </label>
                        <label class="radio-option">
                            <input type="radio" name="q6" value="C" class="text-indigo-600"> C. name
                        </label>
                        <label class="radio-option">
                            <input type="radio" name="q6" value="D" class="text-indigo-600"> D. 报错
                        </label>
                    </div>
                </div>
            </div>

            <!-- 填空题部分 -->
            <div class="question-card p-6 mb-8">
                <h2 class="text-xl font-semibold mb-4 text-gray-800 border-b pb-2">二、填空题（每空3分，共30分）</h2>
                
                <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
                    <div>
                        <p class="font-medium mb-2">1. 创建一个包含数字1、2、3的列表：</p>
                        <input type="text" name="f1" class="answer-input w-full">
                    </div>
                    <div>
                        <p class="font-medium mb-2">2. 元组一旦创建，其元素______（填"可以"或"不可以"）修改</p>
                        <input type="text" name="f2" class="answer-input w-full">
                    </div>
                    <div>
                        <p class="font-medium mb-2">3. 字典是由______和______组成的键值对集合</p>
                        <input type="text" name="f3" class="answer-input w-full">
                    </div>
                    <div>
                        <p class="font-medium mb-2">4. 列表的索引从______开始</p>
                        <input type="text" name="f4" class="answer-input w-full">
                    </div>
                    <div>
                        <p class="font-medium mb-2">5. 要获取列表`[10, 20, 30]`中的第二个元素，应使用______</p>
                        <input type="text" name="f5" class="answer-input w-full">
                    </div>
                    <div>
                        <p class="font-medium mb-2">6. 字典中如果要获取不存在的键，会发生______错误</p>
                        <input type="text" name="f6" class="answer-input w-full">
                    </div>
                    <div>
                        <p class="font-medium mb-2">7. `len([1, 2, 3, 4])`的结果是______</p>
                        <input type="text" name="f7" class="answer-input w-full">
                    </div>
                    <div>
                        <p class="font-medium mb-2">8. 将元素5添加到列表`my_list = [1, 2, 3, 4]`的末尾：______</p>
                        <input type="text" name="f8" class="answer-input w-full">
                    </div>
                    <div>
                        <p class="font-medium mb-2">9. 创建一个包含键值对`'a': 1`的字典：______</p>
                        <input type="text" name="f9" class="answer-input w-full">
                    </div>
                    <div>
                        <p class="font-medium mb-2">10. 元组`(1, 2, 3)`和列表`[1, 2, 3]`的主要区别是______</p>
                        <input type="text" name="f10" class="answer-input w-full">
                    </div>
                </div>
            </div>

            <!-- 简答题部分 -->
            <div class="question-card p-6 mb-8">
                <h2 class="text-xl font-semibold mb-4 text-gray-800 border-b pb-2">三、简答题（每题10分，共20分）</h2>
                
                <div class="mb-6">
                    <p class="font-medium mb-3">1. 简述列表和元组的主要区别。</p>
                    <textarea name="s1" rows="4" class="answer-input w-full"></textarea>
                </div>

                <div class="mb-6">
                    <p class="font-medium mb-3">2. 什么是字典？请举一个生活中的例子说明字典的应用场景。</p>
                    <textarea name="s2" rows="4" class="answer-input w-full"></textarea>
                </div>
            </div>

            <!-- 编程题部分 -->
            <div class="question-card p-6 mb-8">
                <h2 class="text-xl font-semibold mb-4 text-gray-800 border-b pb-2">四、编程题（每题10分，共20分）</h2>
                
                <div class="mb-6">
                    <p class="font-medium mb-3">1. 编写Python代码，创建一个列表`fruits = ['苹果', '香蕉', '橙子']`，然后：</p>
                    <ul class="list-disc pl-5 mb-3">
                        <li>在列表末尾添加'葡萄'</li>
                        <li>打印修改后的列表</li>
                        <li>计算列表的长度并打印</li>
                    </ul>
                    <pre class="code-block mb-3"><code class="language-python" id="code1" contenteditable="true"></code></pre>
                    <textarea name="p1" rows="6" class="answer-input w-full hidden"></textarea>
                </div>

                <div class="mb-6">
                    <p class="font-medium mb-3">2. 编写Python代码，创建一个字典`student = {'name': '小红', 'grade': 5, 'scores': [90, 85, 95]}`，然后：</p>
                    <ul class="list-disc pl-5 mb-3">
                        <li>打印小红的名字</li>
                        <li>计算并打印她的平均分（保留一位小数）</li>
                    </ul>
                    <pre class="code-block mb-3"><code class="language-python" id="code2" contenteditable="true"></code></pre>
                    <textarea name="p2" rows="6" class="answer-input w-full hidden"></textarea>
                </div>
            </div>

            <div class="flex justify-center mt-8">
                <button type="button" id="submitBtn" class="submit-btn px-6 py-3 rounded-lg font-medium shadow-md">
                    <i class="fas fa-paper-plane mr-2"></i>提交试卷
                </button>
            </div>
        </form>

        <!-- 结果展示区域 -->
        <div id="resultSection" class="hidden mt-12">
            <div class="result-card p-6 rounded-lg mb-8">
                <h2 class="text-2xl font-bold mb-4 text-gray-800">测试结果</h2>
                <div class="grid grid-cols-1 md:grid-cols-3 gap-4 mb-6">
                    <div class="bg-white p-4 rounded-lg shadow text-center">
                        <p class="text-gray-500">选择题得分</p>
                        <p class="text-3xl font-bold text-indigo-600" id="choiceScore">0/30</p>
                    </div>
                    <div class="bg-white p-4 rounded-lg shadow text-center">
                        <p class="text-gray-500">填空题得分</p>
                        <p class="text-3xl font-bold text-indigo-600" id="fillScore">0/30</p>
                    </div>
                    <div class="bg-white p-4 rounded-lg shadow text-center">
                        <p class="text-gray-500">总得分</p>
                        <p class="text-3xl font-bold text-indigo-600" id="totalScore">0/100</p>
                    </div>
                </div>
            </div>

            <div class="question-card p-6 mb-8">
                <h2 class="text-xl font-semibold mb-4 text-gray-800 border-b pb-2">参考答案及解析</h2>
                
                <div class="mb-8">
                    <h3 class="text-lg font-medium mb-3 text-gray-700">一、选择题</h3>
                    <div class="space-y-4">
                        <p><span class="font-medium">1.</span> B（元组是不可变数据类型）</p>
                        <p><span class="font-medium">2.</span> C（列表使用方括号[]创建）</p>
                        <p><span class="font-medium">3.</span> B（字典通过键访问值）</p>
                        <p><span class="font-medium">4.</span> B（append()方法用于在列表末尾添加元素）</p>
                        <p><span class="font-medium">5.</span> C（元组使用圆括号()创建）</p>
                        <p><span class="font-medium">6.</span> A（通过键'name'访问字典值，结果为'小明'）</p>
                    </div>
                </div>

                <div class="mb-8">
                    <h3 class="text-lg font-medium mb-3 text-gray-700">二、填空题</h3>
                    <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
                        <p><span class="font-medium">1.</span> [1, 2, 3]</p>
                        <p><span class="font-medium">2.</span> 不可以</p>
                        <p><span class="font-medium">3.</span> 键, 值（或键值对）</p>
                        <p><span class="font-medium">4.</span> 0</p>
                        <p><span class="font-medium">5.</span> [10, 20, 30][1] 或 my_list[1]（假设列表名为my_list）</p>
                        <p><span class="font-medium">6.</span> KeyError</p>
                        <p><span class="font-medium">7.</span> 4</p>
                        <p><span class="font-medium">8.</span> my_list.append(5)</p>
                        <p><span class="font-medium">9.</span> {'a': 1}</p>
                        <p><span class="font-medium">10.</span> 元组不可变，列表可变</p>
                    </div>
                </div>

                <div class="mb-8">
                    <h3 class="text-lg font-medium mb-3 text-gray-700">三、简答题</h3>
                    <div class="space-y-6">
                        <div>
                            <p class="font-medium mb-2">1. 列表和元组的主要区别：</p>
                            <ul class="list-disc pl-5">
                                <li>列表使用[]创建，元组使用()创建</li>
                                <li>列表是可变的（可以修改、添加、删除元素），元组是不可变的（创建后不能修改元素）</li>
                                <li>列表有更多修改元素的方法（如append、insert、remove等），元组没有这些方法</li>
                            </ul>
                        </div>
                        <div>
                            <p class="font-medium mb-2">2. 字典是一种存储键值对的数据类型，通过键来访问对应的值。</p>
                            <p>生活中的例子：电话簿（姓名作为键，电话号码作为值）、学生信息表（学号作为键，学生详细信息作为值）等。</p>
                        </div>
                    </div>
                </div>

                <div>
                    <h3 class="text-lg font-medium mb-3 text-gray-700">四、编程题</h3>
                    <div class="space-y-6">
                        <div>
                            <p class="font-medium mb-2">1. 参考答案：</p>
                            <pre class="code-block"><code class="language-python">fruits = ['苹果', '香蕉', '橙子']
fruits.append('葡萄')
print(fruits)  # 输出：['苹果', '香蕉', '橙子', '葡萄']
print(len(fruits))  # 输出：4</code></pre>
                        </div>
                        <div>
                            <p class="font-medium mb-2">2. 参考答案：</p>
                            <pre class="code-block"><code class="language-python">student = {'name': '小红', 'grade': 5, 'scores': [90, 85, 95]}
print(student['name'])  # 输出：小红
average = sum(student['scores']) / len(student['scores'])
print(f"平均分：{average:.1f}")  # 输出：平均分：90.0</code></pre>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <footer class="bg-gray-100 py-6 mt-12">
        <div class="container mx-auto px-4 text-center text-gray-600">
            <p>created by <a href="https://space.coze.cn" class="text-indigo-600 hover:underline">coze space</a></p>
            <p class="mt-1">页面内容均由 AI 生成，仅供参考</p>
        </div>
    </footer>

    <script>
        document.addEventListener('DOMContentLoaded', function() {
            // 初始化代码高亮
            hljs.highlightAll();
            
            // 保存答案到本地存储
            function saveAnswers() {
                const formData = new FormData(document.getElementById('examForm'));
                const answers = {};
                
                formData.forEach((value, key) => {
                    answers[key] = value;
                });
                
                // 获取编程题代码
                answers['p1'] = document.getElementById('code1').textContent;
                answers['p2'] = document.getElementById('code2').textContent;
                
                localStorage.setItem('pythonExamAnswers', JSON.stringify(answers));
            }
            
            // 加载保存的答案
            function loadAnswers() {
                const savedAnswers = localStorage.getItem('pythonExamAnswers');
                if (savedAnswers) {
                    const answers = JSON.parse(savedAnswers);
                    
                    for (const key in answers) {
                        if (key.startsWith('q')) {
                            // 选择题
                            const radio = document.querySelector(`input[name="${key}"][value="${answers[key]}"]`);
                            if (radio) radio.checked = true;
                        } else if (key.startsWith('f') || key.startsWith('s')) {
                            // 填空题和简答题
                            const input = document.querySelector(`[name="${key}"]`);
                            if (input) input.value = answers[key];
                        } else if (key.startsWith('p')) {
                            // 编程题
                            const codeElement = document.getElementById(key === 'p1' ? 'code1' : 'code2');
                            if (codeElement) codeElement.textContent = answers[key];
                        }
                    }
                }
            }
            
            // 自动保存答案
            document.querySelectorAll('input, textarea').forEach(element => {
                element.addEventListener('input', saveAnswers);
            });
            
            // 编程题内容变化时保存
            document.getElementById('code1').addEventListener('input', saveAnswers);
            document.getElementById('code2').addEventListener('input', saveAnswers);
            
            // 提交试卷
            document.getElementById('submitBtn').addEventListener('click', function() {
                // 计算选择题得分
                const correctChoices = {
                    q1: 'B', q2: 'C', q3: 'B', q4: 'B', q5: 'C', q6: 'A'
                };
                
                let choiceScore = 0;
                for (const [key, value] of Object.entries(correctChoices)) {
                    const selected = document.querySelector(`input[name="${key}"]:checked`);
                    if (selected && selected.value === value) {
                        choiceScore += 5;
                    }
                }
                
                // 计算填空题得分
                const correctFills = {
                    f1: '[1, 2, 3]',
                    f2: '不可以',
                    f3: '键,值', // 允许多种答案
                    f4: '0',
                    f5: '[10, 20, 30][1]', // 允许多种答案
                    f6: 'KeyError',
                    f7: '4',
                    f8: 'my_list.append(5)',
                    f9: "{'a': 1}",
                    f10: '元组不可变，列表可变' // 允许多种表述
                };
                
                let fillScore = 0;
                for (const [key, value] of Object.entries(correctFills)) {
                    const answer = document.querySelector(`[name="${key}"]`).value.trim();
                    
                    // 处理可能有多种答案的情况
                    if (key === 'f3' && (answer.includes('键') && answer.includes('值'))) {
                        fillScore += 3;
                    } else if (key === 'f5' && (answer.includes('[1]') || answer.includes('my_list[1]'))) {
                        fillScore += 3;
                    } else if (key === 'f10' && (answer.includes('不可变') || answer.includes('可变'))) {
                        fillScore += 3;
                    } else if (answer === value) {
                        fillScore += 3;
                    }
                }
                
                // 显示结果
                document.getElementById('choiceScore').textContent = `${choiceScore}/30`;
                document.getElementById('fillScore').textContent = `${fillScore}/30`;
                document.getElementById('totalScore').textContent = `${choiceScore + fillScore}/60`; // 简答题和编程题需要人工评分
                
                // 显示结果区域
                document.getElementById('resultSection').classList.remove('hidden');
                
                // 滚动到结果区域
                document.getElementById('resultSection').scrollIntoView({ behavior: 'smooth' });
            });
            
            // 加载保存的答案
            loadAnswers();
        });
    </script>
</body>
</html>